<?php

class AvaliacaoService extends Model {

    /**
     * @var AvaliacaoDao
     */
    private $avaliacaoDao;
    /**
     * @var AtendimentoService
     */
    private $atendimentoService;
    /**
     * @var ClienteService
     */
    private $clienteService;

    public function injectDependencies() {
        $this->avaliacaoDao = AvaliacaoDao::getInstance();
        $this->atendimentoService = AtendimentoService::getInstance();
        $this->clienteService = ClienteService::getInstance();
    }

    public function findByFilter($IDFilter, $filterType) {
        $Report = array();
        if ($IDFilter == 'list-evaluation-user') {
            $Report = $this->atendimentoService->generateIndividualReport();
        } else if ($IDFilter == 'list-evaluation-sector') {
            $Report = $this->atendimentoService->generateSectorReport();
        } else if ($IDFilter == 'list-evaluation-departament') {
            $Report = $this->atendimentoService->generateDepartamentReport();
        }
        return $Report;
    }

    public function countEvaluationByUser($IDUsuario, $ArPeriod = false) {
        return $this->avaliacaoDao->countEvaluationByUser($IDUsuario, $ArPeriod);
    }

    public function countEvaluationBySector($IDSetor, $ArPeriod = false) {
        return $this->avaliacaoDao->countEvaluationBySector($IDSetor, $ArPeriod);
    }

    public function countEvaluationByDepartament($IDDepartamento, $ArPeriod = false) {
        return $this->avaliacaoDao->countEvaluationByDepartament($IDDepartamento, $ArPeriod);
    }

    public function countEvaluationByCustomer($IDCliente) {
        return $this->avaliacaoDao->countEvaluationByCustomer($IDCliente);
    }

    public function findByCalled($IDChamado) {
        return $this->avaliacaoDao->findEvaluationByCalled($IDChamado);
    }

    public function findByUser($IDUsuario) {
        $ArAvaliacao = $this->avaliacaoDao->findEvaluationsByUser($IDUsuario);
        $this->fillEvaluation($ArAvaliacao);
        return $ArAvaliacao;
    }

    public function findBySector($IDSetor) {
        $ArAvaliacao = $this->avaliacaoDao->findEvaluationsBySector($IDSetor);
        $this->fillEvaluation($ArAvaliacao);
        return $ArAvaliacao;
    }

    public function findByDepartament($IDDepartamento) {
        $ArAvaliacao = $this->avaliacaoDao->findEvaluationsByDepartament($IDDepartamento);
        $this->fillEvaluation($ArAvaliacao);
        return $ArAvaliacao;
    }

    public function generateSatisfactionReport() {
        $ArCustomers = $this->clienteService->findAll();
        $ArSatisfactionReport = array();
        foreach ($ArCustomers as $Customer) {
            $ArSatisfactionReport[] = array(
                'Customer' => $Customer,
                'Called' => $this->atendimentoService->generateCalledReportByCustomer($Customer->getIDCliente()),
                'Evaluations' => $this->countEvaluationByCustomer($Customer->getIDCliente())
            );
        }
        return $ArSatisfactionReport;
    }

    public function countNumberOfEvaluationsType($EnTipoAvaliacao) {
        return $this->avaliacaoDao->countNumberOfEvaluationsType($EnTipoAvaliacao);
    }

    public function generateEvaluationsNumberReport() {
        return $this->avaliacaoDao->countEvaluationsNumberByCustomer();
    }

    private function fillEvaluation(&$MxEvaluation) {
        if (!empty($MxEvaluation)) {
            $this->fillEvaluationWithCalled($MxEvaluation);
        }
    }

    private function fillEvaluationWithCalled(&$MxAvaliacao) {
        if (!is_array($MxAvaliacao)) {
            $MxAvaliacao->setChamado($this->atendimentoService->findById($MxAvaliacao->getIDChamado()));
            return;
        }
        foreach ($MxAvaliacao as &$Avaliacao) {
            $Avaliacao->setChamado($this->atendimentoService->findById($Avaliacao->getIDChamado()));
        }
    }

}

?>
